// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-18

package info

import (
	"context"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/logics/master/ticket/record"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/lib"
)

// Logic
// 设置作钓开始.
type Logic struct {
	record.Logic

	admin  *models.ArcadeAdmins
	arcade *models.Arcades

	member        *models.ArcadeMember
	pond          *models.ArcadePond
	ticket        *models.Ticket
	ticketRecord  *models.TicketRecord
	ticketSession *models.TicketSession
	seat          *models.ArcadePondSeat
	user          *models.Users

	request  *Request
	response *Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: &Response{},
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Query(i, o.request); err == nil {
		o.arcade, o.admin = lib.Arcade.GetMaster().GetArcade(i)
	}
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err error
		res = result.New()
	)

	// 1. 校验数据.
	if err = o.RunCheck(ctx); err != nil {
		return res.WithError(err)
	}

	// 2. 绑定数据.
	o.response.With(o.ticketRecord)
	o.response.WithUser(o.user)
	o.response.WithMember(o.member)
	o.response.WithSession(o.ticketSession)
	o.response.Session.WithTicket(o.ticket)
	o.response.Session.Ticket.WithPond(o.pond)

	// 3. 绑定钓位.
	if o.seat != nil {
		o.response.WithSeat(o.seat)
	}
	return res.With(o.response)
}

// RunCheck
// 查询数据.
func (o *Logic) RunCheck(ctx context.Context) (err error) {
	var has bool

	// 1. 购买记录.
	if o.ticketRecord, has, err = services.NewTicketRecordService().Dao.GetById(ctx, o.request.Id); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 无效记录.
	if !has || o.ticketRecord.ArcadeId != o.arcade.Id {
		err = errs.ErrTicketRecordNotFound
		return
	}

	// 3. 渔票数据.
	if o.ticket, has, err = services.NewTicketService().Dao.GetById(ctx, o.ticketRecord.TicketId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has || o.ticket.ArcadeId != o.arcade.Id || o.ticket.Deleted.IsYes() {
		err = errs.ErrTicketNotFound
		return
	}

	// 4. 鱼塘数据.
	if o.pond, has, err = services.NewArcadePondService().Dao.GetById(ctx, o.ticket.PondId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has || o.pond.ArcadeId != o.arcade.Id || o.pond.Deleted.IsYes() {
		err = errs.ErrArcadePondNotFound
		return
	}

	// 5. 场次数据.
	if o.ticketSession, has, err = services.NewTicketSessionService().Dao.GetById(ctx, o.ticketRecord.SessionId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has || o.ticketSession.ArcadeId != o.arcade.Id || o.ticketSession.Deleted.IsYes() {
		err = errs.ErrTicketSessionNotFound
		return
	}

	// 6. 钓友数据.
	if o.user, has, err = services.NewUsersService().Dao.GetById(ctx, o.ticketRecord.UserId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else if !has {
		err = errs.ErrUserNotFound
		return
	}

	// 7. 钓场会员.
	if o.member, has, err = services.NewArcadeMemberService().Dao.GetByMap(ctx, map[string]any{
		"arcade_id": o.arcade.Id,
		"user_id":   o.ticketRecord.UserId,
	}); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 8. 钓位数据.
	if o.ticketRecord.SeatId > 0 {
		if o.seat, has, err = services.NewArcadePondSeatService().Dao.GetById(ctx, o.ticketRecord.SeatId); err != nil {
			err = errs.ErrDatabaseQuery
			return
		} else if !has || o.seat.ArcadeId != o.arcade.Id || o.seat.Deleted.IsYes() {
			err = errs.ErrArcadeSeatNotFound
			return
		}
	}

	// 9. 校验通过.
	return
}
